<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="单据编号" prop="orderNo">
        <el-input v-model="form.orderNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId">
        <el-select v-model="form.customerId" filterable clearable>
          <el-option
            v-for="item in unitList"
            :key="item.id"
            :label="item.merchantName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="orderStatus">
        <el-select v-model="form.orderStatus" filterable clearable>
          <el-option
            v-for="item in dict.type.ims_receipt_docs_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" v-hasPermi="['ims:payOrder:add']" plain icon="el-icon-plus" @click="updateRow(0,{})">
      新增
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" label="单据编号" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.orderNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="orderDate" label="单据日期" align="center" width="100"/>
      <el-table-column prop="receiptType" label="单据类型" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_receipt_type" :value="scope.row.receiptType"/>
        </template>
      </el-table-column>
      <el-table-column prop="merchantName" label="往来客户" align="center"/>
      <el-table-column prop="orderStatus" label="状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_receipt_docs_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column prop="isCash" label="是否现结" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isCash"/>
        </template>
      </el-table-column>
      <el-table-column prop="incomes" label="收款金额(元)" align="center" width="100"/>
      <el-table-column prop="paidIncomes" label="核销金额(元)" align="center" width="110"/>
      <el-table-column prop="unusedIncomes" label="未使用金额(元)" align="center" width="110"/>
      <el-table-column prop="paymentName" label="支付方式" align="center" width="100"/>
      <el-table-column prop="checkerName" label="业务员" align="center" width="100"/>
      <el-table-column prop="createTime" label="创建时间" align="center" width="140"/>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
         <el-button
           type="text"
           @click="updateRow(1,scope.row)"
         >详情
         </el-button>
          <el-button
            v-if="scope.row.orderStatus==10"
            type="text"
            v-hasPermi="['ims:payOrder:update']"
            @click="updateRow(2,scope.row)"
          >修改
          </el-button>
          <el-button
            v-if="scope.row.orderStatus==10"
            type="text"
            v-hasPermi="['ims:payOrder:delete']"
            @click="updateRow(3,scope.row)"
          >删除
          </el-button>
          <el-button
            v-if="scope.row.orderStatus==10"
            type="text"
            v-hasPermi="['ims:payOrder:approve']"
            @click="updateRow(4,scope.row)"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <updateDialog ref="updateDialog" @ok="handleQuery"></updateDialog>
    <approveDialog ref="approveDialog" @ok="handleQuery"></approveDialog>
  </div>
</template>

<script>
import updateDialog from "./updateDialog";
import {
  listPayOrder,
  deletePayOrder
} from "../../../../api/payMng";
import approveDialog from "./approveDialog.vue";
import {listUnit} from "../../../../api/imsMng";

export default {
  name: 'PayOrder',
  components: {updateDialog, approveDialog},
  dicts: ['ims_receipt_type', 'ims_receipt_docs_status', 'sys_yes_no'],
  data() {
    return {
      loading: false,
      list: [],
      form: {
        orderNo: "",
        merchantName: "",
        orderStatus: "",
        pageNum: 1,
        pageSize: 10
      },
      unitList: [],
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
      listUnit({pageSize: -1}).then(res => {
        this.unitList = res.rows
      })
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    getList() {
      this.loading = false
      listPayOrder(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    updateRow(flag, row) {
      if (flag == 0 || flag == 1 || flag == 2) {
        this.$refs.updateDialog.show(flag, row)
      }
      if (flag == 3) {
        this.$modal.confirm('是否确定删除').then(res => {
          deletePayOrder({id: row.id}).then(res => {
            this.$message.success('删除成功')
            this.handleQuery()
          })
        })
      }
      if (flag == 4) {
        this.$refs.approveDialog.show(row)
      }
    }
  }
}
</script>
